#region Licence
// Copyright (c) 2008 Tasman Logic Ltd

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies 
// or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace ActiveSharp.PropertyMapping
{
    /// <summary>
    /// A helper class to do property change notification.
    /// If all you need is simple property change notification, you can use this class instead of assembling your
    /// own solution using <see cref="PropertyMap"/> directly.
    /// </summary>
    /// <remarks>If you need more advanced behaviour, you can override this class or else write your own alternative to it </remarks>
    public class PropertyChangeHelper
    {
        PropertyChangedEventHandler _propertyChangeDelegate;

        public void Add(PropertyChangedEventHandler value)
        {
            _propertyChangeDelegate += value; ;
        }

        public void Remove(PropertyChangedEventHandler value)
        {
            _propertyChangeDelegate -= value; ;
        }

        void NotifyPropertyChanged(object sender, string propertyName)
        {
            if (_propertyChangeDelegate != null)
                _propertyChangeDelegate(sender, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Sets the field to the new value and raises appropriate notification event
        /// </summary>
        /// <remarks>Does nothing if the new value equals the old</remarks>
        public virtual void SetValue<T>(object containingObject, ref T field, T value)  // note: must have generic type param on contain object, so we can propagate its type to propertyMap
        {
            // only act if the new value is in fact different from the old
            if(!EqualityComparer<T>.Default.Equals(field, value))
            {
                PropertyInfo property = PropertyMap.GetProperty(containingObject, ref field);  // find the property first (i.e. if this blows up, we ain't doin' anything)
                field = value;                                                                 // set the new value before we raise the notification
                NotifyPropertyChanged(containingObject, property.Name);                        // notify that the property has changed
            }
        }
    }
}
